using System;

namespace Glue.Converters
{
    public class QuickConverter<T1,T2>:BaseSimpleConverter<T1,T2>
    {
        private readonly Func<T1, T2> convertTowardsRight;
        private readonly Func<T2, T1> convertTowardsLeft;


        public QuickConverter(Func<T1,T2> convertTowardsRight, Func<T2,T1> convertTowardsLeft)
        {
            this.convertTowardsRight = convertTowardsRight;
            this.convertTowardsLeft = convertTowardsLeft;
        }

        public QuickConverter(Func<T1, T2> convertTowardsRight)
        {
            this.convertTowardsRight = convertTowardsRight;
            convertTowardsLeft = x => {
                 throw new GlueException("Your QuickConverter<" + typeof (T1).Name + "," + typeof (T2).Name +
                                         "> does not convert towardsLeft. Use constructor with two arguments to enable that.");
             };
        }

        public override T2 MapTowardsRight(T1 from)
        {
            return convertTowardsRight(from);
        }

        public override T1 MapTowardsLeft(T2 from)
        {               
            return convertTowardsLeft(from);
        }
    }
}